package com.woke.adapter;

import android.content.res.Resources;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.woke.data.UserPromoteCode;
import com.zhongcai.online.R;

import me.drakeet.multitype.ItemViewProvider;

/**
 * 作者 ： 暗网
 * 创建时间：2017/2/14
 * 备注：
 */

public class ExChangeViewProvider extends ItemViewProvider<UserPromoteCode, ExChangeViewProvider.ViewHolder> {

	private OnItemClickListener mItemClickListener;

	public ExChangeViewProvider(OnItemClickListener listener) {
		mItemClickListener = listener;
	}

	@NonNull
	@Override
	protected ViewHolder onCreateViewHolder(@NonNull LayoutInflater inflater, @NonNull ViewGroup parent) {
		View itemView = inflater.inflate (R.layout.adapter_activity_exchange_item, parent, false);
		return new ViewHolder (itemView);
	}

	@Override
	protected void onBindViewHolder(@NonNull final ViewHolder holder, @NonNull final UserPromoteCode info) {
		Resources rs = holder.itemView.getContext ().getResources ();
		String f_description = rs.getString (R.string.f_description);
		String f_limit = rs.getString (R.string.f_limit);
		String f_interest = rs.getString (R.string.f_interest);
		holder.titleTv.setText (info.getName ());
		if (! TextUtils.isEmpty (info.getDescription ())) {
			holder.subtitleTv.setText (String.format (f_description, info.getDescription ()
					.trim ()));

		}
		if (TextUtils.isEmpty (info.getLimit ())) {
			holder.limitTv.setVisibility (View.GONE);
		} else {
			holder.limitTv.setVisibility (View.VISIBLE);
			holder.limitTv.setText (String.format (f_limit, info.getLimit ().trim ()));
		}
		if (TextUtils.isEmpty (info.getInterest ())) {
			holder.interestTv.setVisibility (View.GONE);
		} else {
			holder.interestTv.setVisibility (View.VISIBLE);
			holder.interestTv.setText (String.format (f_interest, info.getInterest ().trim ()));
		}

		holder.itemView.setOnClickListener (new View.OnClickListener () {
			@Override
			public void onClick(View v) {
				if (mItemClickListener != null) {
					mItemClickListener.OnItemClick (v, getPosition (holder));
					mItemClickListener.OnItemClick (v, info, getPosition (holder));
				}
			}
		});

	}

	class ViewHolder extends RecyclerView.ViewHolder {

		private TextView signIn;
		private TextView titleTv;
		private TextView subtitleTv, limitTv, interestTv;

		public ViewHolder(View itemView) {
			super (itemView);

			signIn = (TextView) itemView.findViewById (R.id.sign_in);
			titleTv = (TextView) itemView.findViewById (R.id.title);
			subtitleTv = (TextView) itemView.findViewById (R.id.subtitle);
			limitTv = (TextView) itemView.findViewById (R.id.limitTv);
			interestTv = (TextView) itemView.findViewById (R.id.interestTv);
		}


	}
}
